// sub_guanzhu/concern/concern.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listLength:'0',
    fzList:'',
    serach_pro: '',
    selectCheckbox: [],
    allSelectChange: [],
    operateIshow: false,
    paixuIsshow:false,
    companyList: [],
    checked: true,
    labelTrue: '全部',
    labelList: [{
      name: '全部',
    }, {
      name: '监控'
    }, {
      name: "走访"
    }, {
      name: '默认分组'
    }],
    fzIshow: false,
    sortRule:'desc',
    total:0,
    pageNum:1,
    groupName:'',
    status:'',
  },
  moveClick(){
    if(this.data.selectCheckbox.length==0){
      wx.showToast({
        icon: "none",
        title: "请选择企业",
      });
    }else{
      this.setData({
        fzIshow:true
      });
    }
   
  },
  onClickCancel(){
    this.setData({
      fzIshow:false
    })
  },
  async onClickHide(){
   const {groupName,status,selectCheckbox}=this.data;
   const param={
    corpList:selectCheckbox,
    groupName:groupName,
    status:status
   };
   const res=await getApp().globalData.request(
    "/api/userApi/changeFocusGroup",
    {...param},
    "POST"
  );
  if(res.code==200){
    this.setData({
      pageNum:1,
      companyList:[],
      selectCheckbox:[],
      operateIshow:false
    })
    wx.showModal({
      title: '操作成功',
      showCancel:false,
    });
    this.getList();
  }
  else{
    wx.showModal({
      title: res.msg,
      showCancel:false,
    })
  }
  this.setData({
    fzIshow:false
  });
  },
  fzChange(e){
    let statuso='';
    switch (e.detail){
      case '默认分组':
        statuso=1
        break;
      case '监控':
        statuso=2
        break;
      case '走访':
        statuso=1
        break;
      default:
        break;
    };
    
    this.setData({
      groupName:e.detail,
      status:statuso
    })
  },
  paixuCheck(e){
    let type=e.currentTarget.dataset.type
    if(type=='true'){
      this.setData({
        paixuIsshow: false,
        operateIshow:false,
      });
    }else{
      this.setData({
        paixuIsshow: true,
        operateIshow:false,
      });
    }
    
  },
  editIsshow(e) {
    let type = e.currentTarget.dataset.type
    if (type == 'false') {
      this.setData({
        paixuIsshow:false,
        operateIshow: true,
        listLength:0
      });
    } else {
      this.setData({
        paixuIsshow:false,
        operateIshow: false,
      
      });
    }
    this.setData({
      selectCheckbox: [],
      allSelectChange: [],
    });
  },
  selectChange(e) {
    this.setData({
      selectCheckbox: e.detail,
      listLength:e.detail.length,
      allSelectChange: []
    });
    if(this.data.selectCheckbox.length==this.data.companyList.length){
      let idlist=[];
      this.data.companyList.map(item=>{
        idlist.push(item.corpCredit);
      });
      this.setData({
        allSelectChange:['all'],
        selectCheckbox:idlist
      });
    }
  },
  allSelectChange(e) {
    this.setData({
      selectCheckbox: [],
      listLength:'0'
    });
    if (e.detail.length != 0) {
      let trueList = []
      this.data.companyList.forEach(element => {
        trueList.push(element.corpCredit)
      });
      this.setData({
        selectCheckbox: trueList,
        listLength:trueList.length
      });
    }

    this.setData({
      allSelectChange: e.detail,
    });

  },
  labelCheck(e) {
    this.setData({
      labelTrue: e.currentTarget.dataset.name,
      operateIshow:false,
      paixuIsshow:false,
      pageNum:1,
      companyList:[],
    });
    this.getList();
  },
  firstSearch(e){
    this.setData({
      serach_pro:e.detail,
      pageNum:1,
      companyList:[]
    });
     this.getList();
  },
  serachProChange(e){
    // if(!e.detail){
    //   this.setData({
    //     serach_pro:"",
    //     pageNum:1,
    //     companyList:[]
    //   })
    //   this.getList();
    // }
  },

  async getList(){
    const {serach_pro,labelTrue,pageNum,sortRule}=this.data;
    const param={
      corpName:serach_pro,
      groupName:labelTrue=='全部'?'':labelTrue,
      sortRule:sortRule,	
      pageNum:pageNum,
      pageSize:10
    };
    const res=await getApp().globalData.request(
      "/api/userApi/queryFocusCompanyList",
      {...param},
      "POST"
    );
    if(res.code==200){
        this.setData({
          pageNum: ++this.data.pageNum,
          companyList: this.data.companyList.concat(res.data?.list || []),
          total:Number(res.data.total),
        })
      
    }
  },
  handleTime(e){
    this.setData({
      sortRule:e.currentTarget.dataset.value,
      pageNum:1,
      companyList:[],
      paixuIsshow:false,
    });
    this.getList();
  },

  handlecancelGZ(){
    wx.showModal({
      title: '提示',
      content: '确定取消关注吗？',
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          const {selectCheckbox}=this.data;
          const param={
            corpList:selectCheckbox
          }
          getApp().globalData.request(
            "/api/userApi/removeFocusCompany",
            {...param},
            "POST"
          ).then(val=>{
              this.setData({
                operateIshow:false,
                selectCheckbox:[],
                pageNum:1,
                companyList:[]
              });
              this.getList();
          })
          
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})